//var host = 'http://zhaoniuw.com:8080/findCows';

var detailedId = '',
    uuid = '',
    detailedId = getQueryString('id'),
    goBack = '';
if(getQueryString('orderBuyhistory')){
    if(window.sessionStorage.getItem('orderBuyindexData')){
        window.sessionStorage.setItem('orderBuyhistory', true)
        goBack = JSON.parse(window.sessionStorage.getItem('orderBuyindexData'))['search'];
    }else {
        window.sessionStorage.removeItem('orderBuyhistory')
    }

}
uuid = Cookie.getCookie('uuid');

var urlType = '',
    dataType = '';
urlType = '/home/getPurchaseInfoDetail';
//接口2.1.45
dataType = "purchaseInfoId=" + detailedId;



askPostData(urlType)
function askPostData(num){

    $.ajax({
        url:host + num,
        type:"post",
        data:dataType,
        success:function(res){
            if(res.success == true){
//				console.log(res)  没有length
                //res为对象，没有
                $('body').show();
                $('#content>div').eq(0).empty();
                createDivOne(res.data);

                share(res.data.meatCategory,res.data.purchaseDesc,null);
            }else{
                //就是无数据则返回什么
                console.log('没有进入')
            }
        }
    });
}
function createDivOne(num){
//	time(num.updateDate)
//	time(updateDate)
//	console.log(num.updateDate)
    $('#mian_Details em').html(time(num.updateDate))
//	$('#content>div').eq(0).append('<p>求购详情</p><div class="content"><div><p>类别</p><p>'+ num.meatCategory +'</p></div><div><p>求购详情</p><p>'+ num.purchaseDesc+'</p></div><div><p>交货地</p><p>'+ num.deliveryArea + '</p></div><div><p>交易员</p><p>'+ num.adminName +'</p></div><div><p>发布人</p><p>'+ num.userName  +'</p>\</div><div><p>数量</p><p>'+ Math.round(num.amount/1000*100)/100 +'</p></div> <div><p>价格(元)</p><p>'+ price = (num.price !='' ? num.price+"/吨" : '议定') +'</p></div> <div><p>单位</p><p>吨</p></div></div>')

    meatCategory = num.meatCategory == '' ? '暂无数据' : num.meatCategory;

    $('#mian_Details h3').html(meatCategory);
//	$('#mian_Details em').html(num.meatCategory);
    price = (num.price !='' ? num.price+"/吨" : '议定')
    $('#mian_Details .active2 em').html('¥'+ price);
    $('#mian_Details .active3 li').eq(0).html('数量：'+ Math.round(num.amount/1000*100)/100 +'吨');


    $('#mian_Details .active3 li').eq(1).html('交货地：'+ num.deliveryArea);
    $('#mian_Details .active3 li').eq(2).html('产地：'+ num.country);
    $('#mian_Details .active3 li').eq(3).html('交易员：'+ num.adminName);


    $('#mian_delivery em').eq(0).html(num.userName);
    $('#mian_delivery em').eq(1).html(num.phone)

    $('#mian_Details2 p').html(num.purchaseDesc)
    //添加电话号码的点击事件
    if(num.phone != ''){
        $('#mian_delivery .active3 a').attr("href",'tel:'+ num.phone);
    }
}


$('#side_delivery_nav li').eq(0).on('click',function(){
    if(window.sessionStorage.getItem('orderBuyhistory')){
        window.location.href = 'orderBuy.html?'+goBack;
    }else {
        window.history.back();
    }

})

//跳转的页面
var textVal = '';
$('#footer_delivery li').eq(0).on('click',function(){
    // 删除历史记录
    window.sessionStorage.removeItem('orderBuyhistory')
    window.sessionStorage.removeItem('orderBuyindexData')
    window.location.href = 'index.html';
})
$('#footer_delivery li').eq(1).on('click',function(){
    $('#sharePage').css({
        'display':'block'
    })
    $('#sharePage').on('click',function(){
        $('#sharePage').css({
            'display':'none'
        })
    })
})

$('#footer_delivery li').eq(2).on('click',function(){
    urlType = '/order/add/withPurchase';
    textVal = $('#textVal').val();
    if(textVal == ''){
        alert("（必填项）供应商信息没有输入")
    }else{
        console.log(uuid)
        DetLogin(fn);
    }
})
//这里判断uuid的状态
function fn(){
    ImporInfor()

    dataType = "purchaseInfoId=" + detailedId + '&content=' + textVal +"&" + Infornation;
    //这是输入窗内的信息
//	console.log(dataType)
    askPostData1(urlType);
}
function askPostData1(num){

    $.ajax({
        url:host + num,
        type:"post",
        data:dataType,
        success:function(res){
            console.log(res)
            if(res.success == true){
//				alert(1)
//				$('body').show();
                $('#orderPage').css({
                    'display':'block'
                });
                sureButton();
            }else{

                alert(res.message)
            }
        }
    });
    function sureButton(){
        $('#orderPage button').on('click',function(){
            window.location.href = 'orderBuy.html';
        })
    }
}

//分享开始
function share(meatCategory,desc,img){

    var titleShare = "求购-"+(meatCategory+"").substring(0,6)+(meatCategory.length>5?"..":"")+" 找牛网清真牛羊肉交易平台";
    if(img==null||img=="undefined"){
        img='http://m.zhaoniuw.com/img/icon_share.jpg';
    }


    $.ajax({
        url:wechathost+shareurl + shareparm,
        type:'post',
        success:function(res){

            var json = eval('(' + res + ')');
            wx.config({
                debug: false, // 开启调试模￿调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印￿
                appId: 'wx554a3c93d558ae03', // 必填，公众号的唯一标识
                timestamp:json.data.timestamp , // 必填，生成签名的时间￿
                nonceStr: json.data.nonceStr, // 必填，生成签名的随机￿
                signature: json.data.signature,// 必填，签名，见附￿
                jsApiList: [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage'


                ] // 必填，鿀要使用的JS接口列表，所有JS接口列表见附￿
            });

            wx.ready(function(){


                wx.checkJsApi({
                    jsApiList: ['onMenuShareAppMessage'], // 需要检测的JS接口列表，所有JS接口列表见附￿
                    success: function(res) {


                        // 以键值对的形式返回，可用的api值true，不可用为false
                        // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                    }
                });
                wx.onMenuShareTimeline({
                    title: titleShare, // 分享标题
                    link:shareUrl,
                    imgUrl: img, // 分享图标
                    success: function () {

                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                    }
                });

                wx.onMenuShareAppMessage({
                    title: titleShare, // 分享标题
                    desc: desc, // 分享描述
                    link: shareUrl, // 分享链接
                    imgUrl: img, // 分享图标
                    type: '', // 分享类型,music、video或link，不填默认为link
                    dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                    success: function () {
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调?ǽ敿
                    }
                });


            });

            wx.error(function(res){

            });


        }
    });
}
//分享结束



//更新时间的
function time(strTime){

    var oldTime = (new Date(strTime)).getTime();   //情求得更新时间
    var date2=new Date();    //当前时间
    var date3= date2.getTime() -oldTime;  //时间差的毫秒数

    //计算出相差天数
    var days=Math.floor(date3/(24*3600*1000))
    //计算出小时数
    var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))
    //计算相差分钟数
    var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000);


    if(hours == 0 && days == 0){
        if(0<minutes && minutes<10){
            strTimeShow = '刚刚';
        }else if(10<=minutes && minutes<60){
            strTimeShow = minutes +'分钟前';
        }
    }else if(hours != 0 && days == 0){
        if(1<=hours && hours<24){
            strTimeShow = hours +'小时前';
        }
    }else if(1<= days && days<365){
//		var arr = str.substring(0,10).split('-');
        strTimeShow = days +'天';
//		strTimeShow = arr[1] + '月' + arr[2] + '日';


        var arr = strTime.substring(0,10).split('-');
        strTimeShow = arr[1] + '月' + arr[2] + '日';
    }else{
        var arr = strTime.substring(0,10).split('-');
        strTimeShow = arr[0] + '年' + arr[1] + '月' + arr[2] + '日';
    }
    return strTimeShow;
}